/**
 * @FileName: StoreSetController.java
 * @Author
 * @Description:
 * @Date 2016年1月6日 下午3:46:52
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.client;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.model.MemberOrg;
import cn.hpclub.server.model.SmsCode;
import cn.hpclub.server.restful.API;
import cn.hpclub.server.service.SmsService;

@API("/SmsServer/SMS")
public class SmsServerController extends BaseClientController<MemberOrg>{
    private static Logger       log      = LoggerFactory.getLogger(SmsServerController.class);
    private final static String TYPE     = "smstype";
    private final static String PHONE    = "phonenumber";
    private final static String PASSWORD = "password";
    private final static String CODE     = "signcode";

    /**
     * @Title: SendSmsCode
     * @Description:发送短信验证码。兼容原接口和保留原接口的扩展能力。保留smstype:1-注册；2-忘记密码，3- 修改邮件等。 4-
     *                                                                扩展为登陆。9-
     *                                                                业务办理
     */
    // ANDROID,IOS也还在使用（终端销售那里没发用新的短信接口）20170518 FLQ，计划V1.6.3迁移
    public void SendSmsCode(){

        JSONObject jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, ClientApiConstant.Msg.FAIL,
                                       ClientApiConstant.Msg.EMPTY);
        try{
            String phone = getPara(PHONE);
            String smstype = getPara(TYPE);
            log.info("SendSmsCode phone=" + phone);
            log.info("SendSmsCode smstype=" + smstype);

            if(!"9".equals(smstype)){// 业务办理时不需要验证人员是否存在和获取次数
                if(!MemberOrg.dao.isExistMemberOrg(phone)){
                    log.error("memberorg is not exist");
                    jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, ClientApiConstant.Msg.ACCOUNT_NOT_EXIST,
                                        ClientApiConstant.Msg.EMPTY);
                    renderText(jo.toString());
                    return;
                }
                if(!SmsCode.dao.countCode(phone)){
                    log.info("get smscode count out of 5 time");
                    jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, ClientApiConstant.Msg.SMS_CODE_COUNT,
                                        ClientApiConstant.Msg.EMPTY);
                    renderText(jo.toString());
                    return;
                }
            }
            JSONObject result = SmsService.service.SendSmsCode(phone);
            if(result.getString(SmsService.service.RZL_RESULT).equals(SmsService.service.RZL_SUCCESS)){
                if(!"9".equals(smstype)){// 业务办理时不需要更新获取次数
                    SmsCode.dao.updateCodeRecord(phone);
                }
                jo = setResponseObj(ClientApiConstant.ResultCode.SUCCESS_CODE,
                                    result.getString(SmsService.service.RZL_MSG), ClientApiConstant.Msg.EMPTY);
            } else{
                jo = setResponseObj(ClientApiConstant.ResultCode.FAILED, result.getString(SmsService.service.RZL_MSG),
                                    ClientApiConstant.Msg.EMPTY);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        renderText(jo.toString());

    }

    // 已完成ESB迁移，并android、IOS已调用V2接口。可废弃-20170518 FLQ
    // public void SendSmsCodeForLogin(){
    //
    // JSONObject jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // ClientApiConstant.Msg.FAIL,
    // ClientApiConstant.Msg.EMPTY);
    // try{
    // String phone = getPara(PHONE);
    // String password = getPara(ClientApiConstant.Key.PASSWORD);
    // // String desPwd = getPara(ClientApiConstant.Key.PTAG);
    // log.info("SendSmsCode phone=" + phone);
    // log.info("SendSmsCode smstype=" + password);
    //
    // MemberOrg member = MemberOrg.dao.getActiveMemberbyUserName(phone);
    // if(member == null){
    // log.error("memberorg is not exist");
    // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // ClientApiConstant.Msg.ACCOUNT_NOT_EXIST,
    // ClientApiConstant.Msg.EMPTY);
    // renderText(jo.toString());
    // return;
    // }
    // // DES获取到原始密码
    // try{
    // password = java.net.URLDecoder.decode(DESCrypto.decrypt(password,
    // ClientApiConstant.CONSTANT_DES_KEY),
    // ClientApiConstant.ENCODING);
    // }
    // catch(Exception e){
    // e.printStackTrace();
    // }
    // String md5Passwd = DigestUtils.md5Hex(password);
    // if(!(member.get("password").equals(md5Passwd))){
    // log.error("password is error");
    // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // ClientApiConstant.Msg.ACCOUNT_NOT_EXIST,
    // ClientApiConstant.Msg.EMPTY);
    // renderText(jo.toString());
    // return;
    // }
    //
    // if(!SmsCode.dao.countCode(phone)){
    // log.info("get smscode count out of 5 time");
    // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // ClientApiConstant.Msg.SMS_CODE_COUNT,
    // ClientApiConstant.Msg.EMPTY);
    // renderText(jo.toString());
    // return;
    // }
    //
    // String empCode = member.getStr(MemberOrg.EMP_CODE);
    // JSONObject result = SmsService.service.sendOrCheckSmsCode(phone, empCode,
    // 0, "");
    // long returnCode = result.getJSONObject("ROOT").getLong("RETURN_CODE");
    // if(returnCode == 0){
    // SmsCode.dao.updateCodeRecord(phone);
    // jo = setResponseObj(ClientApiConstant.ResultCode.SUCCESS_CODE,
    // result.getJSONObject("ROOT").getString("USER_MSG"),
    // ClientApiConstant.Msg.EMPTY);
    // } else{
    // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // result.getJSONObject("ROOT").getString("USER_MSG"),
    // ClientApiConstant.Msg.EMPTY);
    // }
    // }
    // catch(Exception e){
    // e.printStackTrace();
    // }
    // renderText(jo.toString());
    // }

    // 该方法ANDROID和IOS都没有在用，可废弃-20170518,FLQ
    // public void VerifySignCode(){
    //
    // JSONObject jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // ClientApiConstant.Msg.FAIL,
    // ClientApiConstant.Msg.EMPTY);
    // try{
    // String phone = getPara(PHONE);
    // String code = getPara(CODE);
    // // String phone = "13880936003";
    // // String code = "123456";
    // // log.info("VerifySignCode phone=" + phone);
    // // log.info("VerifySignCode code=" + code);
    //
    // if(!MemberOrg.dao.isExistMemberOrg(phone)){
    // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // ClientApiConstant.Msg.ACCOUNT_NOT_EXIST,
    // ClientApiConstant.Msg.EMPTY);
    // renderText(jo.toString());
    // return;
    // }
    // JSONObject result = SmsService.service.VerifySignCode(phone, code);
    //
    // // JSONObject result = new JSONObject();
    // // result.put(SmsService.service.RZL_RESULT, "0000000");
    // // result.put(SmsService.service.RZL_MSG, "dl");
    //
    // if(result.getString(SmsService.service.RZL_RESULT).equals(SmsService.service.RZL_SUCCESS)){
    // if(SmsCode.dao.updateCode(phone, code)){
    // jo = setResponseObj(ClientApiConstant.ResultCode.SUCCESS_CODE,
    // result.getString(SmsService.service.RZL_MSG),
    // ClientApiConstant.Msg.EMPTY);
    // } else{
    // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // ClientApiConstant.Msg.SMS_CODE_ERROR,
    // ClientApiConstant.Msg.EMPTY);
    // }
    // } else{
    // jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
    // result.getString(SmsService.service.RZL_MSG),
    // ClientApiConstant.Msg.EMPTY);
    // }
    // }
    // catch(Exception e){
    // e.printStackTrace();
    // }
    //
    // renderText(jo.toString());
    //
    // }

    /*
     * public void login(){ JSONObject jo =
     * setResponseObj(ClientApiConstant.ResultCode.FAILED,
     * ClientApiConstant.Msg.FAIL, ClientApiConstant.Msg.EMPTY); try{ String
     * phone = getPara(PHONE); String code = getPara(CODE);
     * 
     * if(!MemberOrg.dao.isExistMemberOrg(phone)){ log.error(
     * "validate failed.userName=" + phone); jo =
     * setResponseObj(ClientApiConstant.ResultCode.NOT_EXIST,
     * ClientApiConstant.Msg.ACCOUNT_NOT_EXIST_OR_PASSWD_ERROR,
     * ClientApiConstant.Msg.EMPTY); renderText(jo.toString()); return; }
     * 
     * JSONObject result = SmsService.service.VerifySignCode(phone, code);
     * 
     * if(!result.getString(SmsService.service.RZL_RESULT).equals(SmsService.
     * service.RZL_SUCCESS)){ jo =
     * setResponseObj(ClientApiConstant.ResultCode.FAILED,
     * result.getString(SmsService.service.RZL_MSG),
     * ClientApiConstant.Msg.EMPTY); renderText(jo.toString()); return; }
     * 
     * String token = TokenService.service.getToken(phone);
     * if(StrKit.isBlank(token)){ log.error("token is empty. userName=" +
     * phone); jo = setResponseObj(ClientApiConstant.ResultCode.FAILED,
     * ClientApiConstant.Msg.EXCEPTION, ClientApiConstant.Msg.EMPTY);
     * renderText(jo.toString()); return; }
     * 
     * JSONObject data = new LoginService(phone, "").getData(token); jo =
     * setResponseObj(ClientApiConstant.ResultCode.SUCCESS_CODE,
     * ClientApiConstant.Msg.SUCCESS, data);
     * 
     * } catch(IOException | JSONException e){ e.printStackTrace(); jo =
     * setResponseObj(ClientApiConstant.ResultCode.FAILED,
     * ClientApiConstant.Msg.MSG_EXCEPTION, ClientApiConstant.Msg.EMPTY); }
     * catch(VerifyEmpCodeException e){ e.printStackTrace(); jo =
     * setResponseObj(ClientApiConstant.ResultCode.FAILED,
     * ClientApiConstant.Msg.CITY_CODE_ID_ERROR, ClientApiConstant.Msg.EMPTY); }
     * catch(EmpCodeIsEmptyException e){ e.printStackTrace(); jo =
     * setResponseObj(ClientApiConstant.ResultCode.FAILED,
     * ClientApiConstant.Msg.EMP_CODE_ERROR, ClientApiConstant.Msg.EMPTY); }
     * catch(VerifyEmpCode9999Exception e){ e.printStackTrace(); jo =
     * setResponseObj(ClientApiConstant.ResultCode.FAILED, e.getMessage(),
     * ClientApiConstant.Msg.EMPTY); }
     * 
     * renderText(jo.toString()); }
     */
}
